import { _decorator, Component, log, Node } from 'cc';
import InputListenerMgr from '../../core/InputListenerMgr';
import { KeyEventListener } from '../../core/GameInputListeners';
import { InputKeyCode } from '../../core/InputConfig';
import GameInputEvent from '../../core/GameInputEvent';



const { ccclass, property } = _decorator;
/**
 * 搜集一个节点上的KeyEventListener
 */
@ccclass('NodeKeyBoardProcessor')
export class NodeKeyBoardProcessor extends Component {
    // @property({ tooltip: DEV && "是否归keyboard管理" })
    // @property
    isBelongKeyBoard: boolean = false;
    //按键监听队列
    protected keyList: InputListenerMgr<KeyEventListener> = new InputListenerMgr();
    protected _isBlock: { [key: number]: boolean } = {};
    protected _curKeyCode: InputKeyCode = InputKeyCode.NONE;
    // isBlockInput(): boolean {
    //     return this._isBlock[this._curKeyCode];
    // }
    public get isActive() {
        // LogUtils.log('KeyCodeHandler  isActive ', this.node.activeInHierarchy, this.activeInHierarchy)
        return this.node.isValid && this.node.activeInHierarchy;
    }

    get isDestroy() {
        return this.node == null || !this.node.isValid;
    }


    onKeyBoardPress(keyCode: InputKeyCode, event?: GameInputEvent): KeyEventListener {
        if (!this.isActive) {
            return null;
        }
        this._isBlock[keyCode] = false;
        let list = this.keyList.list;
        for (let index = list.length - 1; index >= 0; index--) {
            const element = list[index];
            element.onKeyBoardPress(keyCode, event)
            if (this.isDestroy) {
                log('onKeyBoardPress is destroy name is ', this.node?.name)
                return element;
            } else {
                if (element.isBlockInput()) {
                    this._isBlock[keyCode] = true;
                    this._curKeyCode = keyCode;
                    log('按下的按键节点是 :', keyCode, this.node.name)
                    return element
                    // break;
                }
            }

        }
        return null;
    }

    onKeyBoardRelease(keyCode: InputKeyCode, event?: GameInputEvent): KeyEventListener {
        // if (!this._isBlock[keyCode]) {
        //     return null;
        // }
        if (!this.isActive) {
            log('非激活状态 :', keyCode, this.node.name)
            return null;
        }
        this._isBlock[keyCode] = false;
        let list = this.keyList.list;
        for (let index = list.length - 1; index >= 0; index--) {
            const element = list[index];
            element.onKeyBoardRelease(keyCode, event)
            // if (this.node.name == "PCLoginLayout") {
            //     LogUtils.log(' this.isDestroy ', this.isDestroy)
            // }
            if (this.isDestroy) {
                log('onKeyBoardRelease is destroy name is ', this.node?.name)
                return element;
            } else {
                // if (this.node.name == "PCLoginLayout") {
                //     LogUtils.log(' element.isBlockInput() ', element.isBlockInput())
                // }
                if (element.isBlockInput()) {
                    this._isBlock[keyCode] = true;
                    this._curKeyCode = keyCode;
                    log('抬起的按键节点是 :', keyCode, this.node.name)
                    return element
                }
            }

        }
        return null;
    }


    addKeyEventListener(listener: KeyEventListener) {
        if (this.keyList)
            this.keyList.on(listener)
    }

    removeKeyEventListener(listener: KeyEventListener) {
        if (this.keyList)
            this.keyList.off(listener)
    }
}